<?php
/*
//////////////////////////////////////////////////////////
//  SUPER ORDERS                                        //
//                                                      //
//  By Frank Koehl (PM: BlindSide)                      //
//                                                      //
//  Powered by Zen-Cart (www.zen-cart.com)              //
//  Portions Copyright (c) 2005 The Zen-Cart Team       //
//                                                      //
//  Released under the GNU General Public License       //
//  available at www.zen-cart.com/license/2_0.txt       //
//  or see "license.txt" in the downloaded zip          //
//////////////////////////////////////////////////////////
//  FILENAME:      super_upgrade.php                    //
//                                                      //
//  DESCRIPTION:   Converts payment data from the 1.0   //
//  to the new 2.0 format by moving data into the new   //
//  "SO_" tables.  The old data is not touched in any   //
//  way, so its integrity is ensured.  Credit card      //
//  payments are also converted into standard SO        //
//  payment line items.                                 //
//////////////////////////////////////////////////////////
*/

require('includes/application_top.php');
$action = (isset($_GET['action']) ? $_GET['action'] : 'confirm');
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/cssjsmenuhover.css" media="all" id="hoverJS">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script type="text/javascript">
  <!--
  function init()
  {
    cssjsmenu('navbar');
    if (document.getElementById)
    {
      var kill = document.getElementById('hoverJS');
      kill.disabled = true;
    }
  }
  // -->
</script>
</head>
<body onload="init()">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<?php
if ($action == 'confirm') {
?>
  <tr>
    <td class="pageHeading"><?php echo 'Super Orders DB Upgrade' ?></td>
    <td class="pageHeading" align="right"><?php echo zen_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
  </tr>
  <tr>
    <td class="main" colspan="2">This program will migrate your Super Orders payment data to the new table format.
    <br /><b>Your old data will not be touched</b>.</td>
  <tr>
  <tr>
    <td class="alert" colspan="2">You must run the SQL file included in the zip file first!</td>
  </tr>
  <tr>
    <td class="main">When you are ready, press the Submit button below.</td>
  </tr>
  <tr>
    <td><br /><INPUT TYPE="BUTTON" VALUE="SUBMIT" ONCLICK="window.location.href='/admin/super_upgrade.php?action=upgrade'"></td>
  </tr>
<?php
}
elseif ($action == 'upgrade') {

  global $db;
  require(DIR_WS_CLASSES . 'super_order.php');

  // migrate PO's from payment_purchase_order to so_purchase_orders
  $po = $db->Execute("select * from payment_purchase_order");
  while(!$po->EOF) {
    unset($new_po);
    $new_po = array();

    $new_po['purchase_order_id'] = $po->fields['po_index'];
    $new_po['orders_id'] = $po->fields['orders_id'];
    $new_po['po_number'] = $po->fields['po_number'];
    $new_po['date_posted'] = $po->fields['date_invoiced'];
    $new_po['last_modified'] = $po->fields['last_modified'];

    zen_db_perform(TABLE_SO_PURCHASE_ORDERS, $new_po);
    $po->MoveNext();
  }

  print('PO migration complete!<br><br>');

  // migrate the data from payment_check to so_payments
  $payments = $db->Execute("select * from payment_check");
  while (!$payments->EOF) {
    unset($new_payment);
    $new_payment = array();

    $new_payment['payment_id'] = $payments->fields['check_index'];
    $new_payment['orders_id'] = $payments->fields['orders_id'];
    $new_payment['payment_number'] = $payments->fields['check_number'];
    $new_payment['payment_name'] = $payments->fields['check_name'];
    $new_payment['payment_amount'] = $payments->fields['check_amount'];
    $new_payment['payment_type'] = 'CK';
    $new_payment['date_posted'] = $payments->fields['date_posted'];
    $new_payment['last_modified'] = $payments->fields['last_modified'];

    // look for attached PO's
    $attached_po = $db->Execute("select purchase_order_id from " . TABLE_SO_PURCHASE_ORDERS . "
                                 where orders_id = '" . $new_payment['orders_id'] . "'");
    if (zen_not_null($attached_po->fields['purchase_order_id'])) {
      $new_payment['purchase_order_id'] = $attached_po->fields['purchase_order_id'];
    }

    zen_db_perform(TABLE_SO_PAYMENTS, $new_payment);
    $payments->MoveNext();
  }

  print('Payment migration complete!<br><br>');

  // migrate the extra check data from payment_check_balance to so_payments
  $extra_checks = $db->Execute("select * from payment_check_balance
                                where check_direction = 1");
  while (!$extra_checks->EOF) {
    unset($new_check);
    $new_check = array();

    $new_check['orders_id'] = $extra_checks->fields['orders_id'];
    $new_check['payment_number'] = $extra_checks->fields['check_number'];
    $new_check['payment_name'] = $extra_checks->fields['check_name'];
    $new_check['payment_amount'] = $extra_checks->fields['check_amount'];
    $new_check['payment_type'] = 'CK';
    $new_check['date_posted'] = $extra_checks->fields['date_posted'];
    $new_check['last_modified'] = $extra_checks->fields['last_modified'];

    // look for attached PO's
    unset($attached_po);
    $attached_po = $db->Execute("select purchase_order_id from " . TABLE_SO_PURCHASE_ORDERS . "
                                 where orders_id = '" . $new_check['orders_id'] . "'");
    if (zen_not_null($attached_po->fields['purchase_order_id'])) {
      $new_check['purchase_order_id'] = $attached_po->fields['purchase_order_id'];
    }

    zen_db_perform(TABLE_SO_PAYMENTS, $new_check);
    $extra_checks->MoveNext();
  }

  print('Extra check migration complete!<br><br>');

  // migrate the refund check data from payment_check_balance to so_refunds
  $refund_checks = $db->Execute("select * from payment_check_balance
                                where check_direction = -1");
  while (!$refund_checks->EOF) {
    unset($new_refund);
    $new_refund = array();

    $new_refund['payment_id'] = $refund_checks->fields['check_index'];
    $new_refund['orders_id'] = $refund_checks->fields['orders_id'];
    $new_refund['refund_number'] = $refund_checks->fields['check_number'];
    $new_refund['refund_name'] = $refund_checks->fields['check_name'];
    $new_refund['refund_amount'] = $refund_checks->fields['check_amount'];
    $new_refund['refund_type'] = 'CK';
    $new_refund['date_posted'] = $refund_checks->fields['date_posted'];
    $new_refund['last_modified'] = $refund_checks->fields['last_modified'];

    zen_db_perform(TABLE_SO_REFUNDS, $new_refund);
    $refund_checks->MoveNext();
  }

  print('Refund migration complete!<br><br>');
  // copy CC data from fields in orders to so_payments
  $cc_orders = $db->Execute("select orders_id from " . TABLE_ORDERS . " where
                             cc_type != '' OR
                             cc_owner != '' OR
                             cc_number != ''");

  while (!$cc_orders->EOF) {
    $orders_id = $cc_orders->fields['orders_id'];

    $cc_data = $db->Execute("select cc_type, cc_owner, cc_number, cc_expires, cc_cvv, date_purchased, order_total
                             from " . TABLE_ORDERS . " where orders_id = '" . $orders_id . "' limit 1");

    $new_cc_payment = array('orders_id' => $orders_id,
                            'payment_number' => $cc_data->fields['cc_number'],
                            'payment_name' => $cc_data->fields['cc_owner'],
                            'payment_amount' => $cc_data->fields['order_total'],
                            'payment_type' => $cc_data->fields['cc_type'],
                            'date_posted' => $cc_data->fields['date_purchased'],
                            'last_modified' => $cc_data->fields['date_purchased']);


    zen_db_perform(TABLE_SO_PAYMENTS, $new_cc_payment);

    $cc_orders->MoveNext();
  }

  print('Credit card data conversion complete!<br>');

  // put the balance due into the derived field in orders table
  print('<br>Now updating orders table.  This could take a while...<br>');
  $all_orders = $db->Execute("select * from " . TABLE_ORDERS . " order by orders_id asc");
  while (!$all_orders->EOF) {
    $so = new super_order($all_orders->fields['orders_id']);
    $so->new_balance();
    unset($so);
    $all_orders->MoveNext();
  }
  print('<br>Orders table update complete!');

?>
  <tr>
    <td align="center" class="pageHeading">UPGRADE COMPLETE!</td>
  <tr>
  <tr>
    <td align="center" class="main">Welcome to Super Orders v 2.0</td>
  <tr>
  <tr>
    <td align="left" class="alert"><br /><u>REMEMBER!</u></td>
  <tr>
  <tr>
    <td align="left" class="alert">Remove this file once you have checked your data!</td>
  <tr>
  <tr>
    <td align="left" class="alert">Add the CC code to /includes/modules/pages/checkout_success/header_php.php, Line 97:</td>
  <tr>
  <tr>
    <td align="left"><font face=Courier>  //SO_EDIT_START<br />
  // Add the CC payment to the Super Orders payment system<br />
  $cc_insert = $db->Execute("select cc_type, cc_owner, cc_number, cc_expires, cc_cvv, date_purchased, order_total from " . TABLE_ORDERS . " where orders_id = '" . $zv_orders_id . "' limit 1");<br /><br />
  if (zen_not_null($cc_insert->fields['cc_number'])) {<br />
    $new_cc_payment = array('orders_id' => $zv_orders_id,<br />
                            'payment_number' => $cc_data->fields['cc_number'],<br />
                            'payment_name' => $cc_data->fields['cc_owner'],<br />
                            'payment_amount' => $cc_data->fields['order_total'],<br />
                            'payment_type' => $cc_data->fields['cc_type'],<br />
                            'date_posted' => $cc_data->fields['date_purchased'],<br />
                            'last_modified' => $cc_data->fields['date_purchased']);<br />

    zen_db_perform(TABLE_SO_PAYMENTS, $new_cc_payment);<br />
  }<br />
  //SO_EDIT_END</font></td>
  <tr>
<?php
}
?>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>